<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">-->
    <title>linecap</title>
</head>
<body>

<canvas id="canvas" ></canvas>
<script>
    function draw() {
        var ctx = document.getElementById('canvas').getContext('2d');
        var lineCap = ['butt', 'round', 'square'];
        //默认butt
        //round线段两端椭圆。半径一半线宽
        //square线段两头多绘制一半线宽高度的线段

        // 创建路径
        ctx.strokeStyle = '#09f';
        ctx.beginPath();
        ctx.moveTo(10, 10);
        ctx.lineTo(140, 10);
        ctx.moveTo(10, 140);
        ctx.lineTo(140, 140);
        ctx.stroke();

        // 画线条
        ctx.strokeStyle = 'black';
        for (var i = 0; i < lineCap.length; i++) {
            ctx.lineWidth = 15;
            ctx.lineCap = lineCap[i];
            ctx.beginPath();
            ctx.moveTo(25 + i * 50, 10);
            ctx.lineTo(25 + i * 50, 140);
            ctx.stroke();
        }
    }
    draw();
</script>
</body>
</html>